package ewgenius.xube.engine.graphics;

import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Vector3f;

import ewgenius.xube.engine.Settings;

/*
 * +y - top, -y - bottom
 * +x - right, -x - left
 * +z - back, -z - forward
 */

public class Camera {
	private Vector3f position = new Vector3f(0, 0, 0);
	private float yaw = 0; // rotation around x
	private float pitch = 0; // rotation around y
	private float roll = 0; // rotation around z
	private float zNear = 0.0f;
	private float zFar = 200000f;
	private int width;
	private int height;

	public Camera(Settings settings) {
		width = settings.screenWidth;
		height = settings.screenHeight;
		zFar = settings.viewDistance;

		if (settings.fullscreen) {
			width = Display.getDesktopDisplayMode().getWidth();
			height = Display.getDesktopDisplayMode().getHeight();
		}
	}

	public void initialize() {
		glViewport(0, 0, width, height);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(30f, width / height, zNear, zFar);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}

	public void setPosition(float xcoord, float ycoord, float zcoord) {
		position.x = xcoord;
		position.y = ycoord;
		position.z = zcoord;
	}

	public void setRotation(float y, float p, float r) {
		yaw = y;
		pitch = p;
		roll = r;
	}

	public Vector3f getPosition() {
		return position;
	}

	public Vector3f getRotation() {
		return new Vector3f(yaw, pitch, roll);
	}

	public void update() {
		glLoadIdentity();
		glRotatef(yaw, 1, 0, 0);
		glRotatef(pitch, 0, 1, 0);
		glRotatef(roll, 0, 0, 1);
		glTranslatef(position.x, position.y, position.z);
	}
}
